package jwlee.testjson;

import java.io.FileOutputStream;
import java.io.PrintWriter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;
import net.sf.json.xml.XMLSerializer;

public class JsonTest {
	public static void main(String[] args) throws Exception {
		JSONStringer js = new JSONStringer();
		JSONObject obj2 = new JSONObject();
		JSONObject obj3 = new JSONObject();
		JSONObject obj4 = new JSONObject();
		obj4.put("title", "book1");
		obj4.put("price", "$11");
		obj3.put("book", obj4);
		obj3.put("author", new JSONObject().put("name", "author-1"));

		JSONObject obj5 = new JSONObject();
		JSONObject obj6 = new JSONObject();
		obj6.put("title", "book2");
		obj6.put("price", "$22");
		obj5.put("book", obj6);
		obj5.put("author", new JSONObject().put("name", "author-2"));

		JSONArray obj7 = new JSONArray();
		obj7.add(obj3);
		obj7.add(obj5);

		obj2.put("title", "BOOK");
		obj2.put("signing", obj7);

		js.object().key("session").value(obj2).endObject();
		System.out.println(js.toString());
		
		//xml -> json
		String xml = "<person> <name>xiazdong</name> <age>20</age> </person>";
		JSONObject json = (JSONObject)new XMLSerializer().read(xml);
		System.out.println(json.toString());
	}

}
